package com.shenganyuan.smac.contact;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.oklib.base.CoreBaseFragment;
import com.oklib.utils.view.SnackbarUtil;
import com.oklib.widget.recyclerview.BaseQuickAdapter;
import com.oklib.widget.recyclerview.BaseViewHolder;
import com.oklib.widget.recyclerview.CoreRecyclerView;
import com.oklib.widget.recyclerview.listener.OnItemChildClickListener;
import com.oklib.widget.recyclerview.listener.OnItemClickListener;
import com.shenganyuan.smac.R;
import com.shenganyuan.smac.common.utils.PhoneUtil;
import com.shenganyuan.smac.contact.contract.ContactContract;
import com.shenganyuan.smac.contact.model.ApiContactBean;
import com.shenganyuan.smac.contact.model.ApiMainContact;
import com.shenganyuan.smac.contact.model.ContactModel;
import com.shenganyuan.smac.contact.presenter.ContactPresenter;
import com.shenganyuan.smac.msg.ActMsgChat;
import com.shenganyuan.smac.msg.model.BeanChatTo;
import com.shenganyuan.smac.common.view.RecyclerViewItemDivider;

import java.util.List;

import butterknife.BindView;

/**
 * @author Damon
 * @date 2017/12/14
 */

public class FrgContact extends CoreBaseFragment<ContactPresenter, ContactModel> implements ContactContract.View {
    @BindView(R.id.main_contact_recycler)
    CoreRecyclerView recycler;

    public static FrgContact newInstance() {
        Bundle args = new Bundle();
        FrgContact fragment = new FrgContact();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public int getLayoutId() {
        return R.layout.main_frg_contact;
    }

    @Override
    public void initUI(View view, @Nullable Bundle savedInstanceState) {
        recycler.getRecyclerView().addItemDecoration(new RecyclerViewItemDivider(mContext, LinearLayoutManager.VERTICAL)
                .setAppearance(1, "#eeeeee"));
        recycler.init(new BaseQuickAdapter<ApiContactBean, BaseViewHolder>(R.layout.contact_item_main) {
            @Override
            protected void convert(BaseViewHolder helper, ApiContactBean item) {
                helper.setText(R.id.name, item.getName());
                helper.setText(R.id.mobile, item.getTel());
                helper.addOnClickListener(R.id.mImageChat);
                helper.addOnClickListener(R.id.mImagePhone);
            }
        });
        recycler.addOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                ApiContactBean item = (ApiContactBean) adapter.getItem(position);
                PhoneUtil.callPhone(getActivity(), item.getTel());
            }
        });
        recycler.addOnChildItemClickListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                ApiContactBean item = (ApiContactBean) adapter.getItem(position);
                switch (view.getId()) {
                    case R.id.mImageChat:
                        toChat(item);
                        break;
                    case R.id.mImagePhone:
                        PhoneUtil.callPhone(getActivity(), item.getTel());
                        break;
                }
            }
        });
        recycler.openRefresh(new CoreRecyclerView.addDataListener() {
            @Override
            public void addData(int page) {
                initData();
            }
        });

    }

    private void toChat(ApiContactBean item) {
        Bundle bundle = new Bundle();
        BeanChatTo beanChatTo = new BeanChatTo();
        beanChatTo.id = item.getDjUserId();
        beanChatTo.name = item.getName();
        bundle.putSerializable("beanChatTo", beanChatTo);

        Intent intent = new Intent();
        intent.putExtras(bundle);
        intent.setClass(getContext(), ActMsgChat.class);
        startActivity(intent);
    }

    @Override
    public void initData() {
        mPresenter.getContacts();
    }


    @Override
    public void showContent(ApiMainContact apiContact) {
        if (apiContact == null) {
            return;
        }
        List<ApiContactBean> datas = apiContact.getMessage();
        recycler.getAdapter().addData(datas);
    }

    @Override
    public void showMsg(String msg) {
        SnackbarUtil.showShort(mainView, msg);
    }
}
